#define _CRT_SECURE_NO_WARNINGS 1
#include "SingleList.h"
#include "Contact.h"

SingleListNode* head = NULL;//头节点

struct Contact people;

void menu()
{
	printf("****************************************\n");
	printf("****1.添加联系人        2.删除联系人****\n");
	printf("****3.查找联系人        4.打印通讯录****\n");
	printf("****5.修改联系人        6.保存通讯录****\n");
	printf("****7.加载通讯录        8.退出通讯录****\n");
	printf("****************************************\n");

}

int main()
{
	char ch = 0;
	do
	{
		menu();
		ch = _getch();
		switch (ch)
		{
		case '1':
			//添加联系人
			PushPeople();
			break;
		case '2':
			//删除联系人
			DeletePeople();
			break;
		case '3':
			//查找联系人
			FindPeople();
			break;
		case '4':
			//打印通信录
			Print();
			break;
		case '5':
			//修改联系人
			ChangePeople();
			break;
		case '6':
			//保存通讯录
			SavePeople();
			break;
		case '7':
			//加载通讯录
			InitPeople();
			break;
		case '8':
		{
			//退出通信录
			printf("退出通讯录！\n");
			exit(1);
			break;
		}
		default:
			printf("非法选择，请重新选择：\n");
			break;
		}
		system("pause");
		system("cls");
	} while (ch);

	return 0;
}

//查找联系人
SingleListNode* FindPeople()
{
	printf("请输入要查找的人的姓名:\n");
	char name[20] = { 0 };
	scanf("%s", name);

	SingleListNode* cur = head;
	while (cur)
	{
		if (0 == strcmp(cur->data.name, name))
		{
			printf("%s %d %s %s %s\n",
				cur->data.name,
				cur->data.age,
				cur->data.sex,
				cur->data.phonenumber,
				cur->data.address);
			return cur;
		}
		cur = cur->next;
	}
	printf("查无此人！");
	return NULL;
}


//添加联系人
void PushPeople()
{
	printf("请输入联系人的姓名：\n");
	scanf("%s", people.name);
	printf("请输入联系人的年龄：\n");
	scanf("%d", &people.age);
	printf("请输入联系人的性别：\n");
	scanf("%s", people.sex);
	printf("请输入联系人的手机号:\n");
	scanf("%s", people.phonenumber);
	printf("请输入联系人的住址：\n");
	scanf("%s", people.address);

	SingleListPushBack(&head, people);
	
	printf("添加成功!");
}

//删除联系人
void DeletePeople()
{
	printf("请输入要删除的联系人姓名：\n");
	char name[20] = { 0 };
	scanf("%s", name);

	system("cls");

	SingleListNode* cur = head;
	while (cur)
	{
		if (0 == strcmp(cur->data.name, name))
		{
			printf("%s %d %s %s %s\n",
				cur->data.name,
				cur->data.age,
				cur->data.sex,
				cur->data.phonenumber,
				cur->data.address);
			int a = 0;
			printf("是否要删除？\n");
			printf("1.yes  2.no\n");
			scanf("%d", &a);
			if (a == 1)
			{
				SingleListDeletePos(&head, cur);
				printf("删除成功！\n");
				break;
			}
			else
			{
				printf("取消删除成功！");
				break;
			}
		}
		cur = cur->next;
	}
	if (cur == NULL)
	{
		printf("查无此人！");
	}
}

//打印通讯录
void Print()
{
	system("cls");
	printf("****************************************\n");
	printf("*姓名\t年龄\t性别\t电话\t住址\t*\n");
	printf("****************************************\n");
	SingleListNode* cur = head;
	while (cur)
	{
		printf("%s\t%d\t%s\t%s\t%s\n",
			cur->data.name,
			cur->data.age,
			cur->data.sex,
			cur->data.phonenumber,
			cur->data.address);
		cur = cur->next;
	}
}

//修改联系人
void ChangePeople()
{
	printf("请输入要修改的联系人姓名：\n");
	char name[20] = { 0 };
	scanf("%s", name);

	system("cls");

	SingleListNode* cur = head;
	while (cur)
	{
		if (0 == strcmp(cur->data.name, name))
		{
			printf("%s %d %s %s %s\n",
				cur->data.name,
				cur->data.age,
				cur->data.sex,
				cur->data.phonenumber,
				cur->data.address);
			printf("请输入要修改姓名：\n");
			scanf("%s", cur->data.name);
			printf("请输入要修改的年龄：\n");
			scanf("%d", &cur->data.age);
			printf("请输入要修改的性别：\n");
			scanf("%s", cur->data.sex);
			printf("请输入要修改的手机号:\n");
			scanf("%s", cur->data.phonenumber);
			printf("请输入要修改的住址：\n");
			scanf("%s", cur->data.address);
			printf("修改成功！\n");
			break;
		}
		cur = cur->next;
	}
	if (cur == NULL)
	{
		printf("查无此人！");
	}
}


//保存通讯录
void SavePeople()
{
	SingleListNode* cur = head;
	//打开文件
	FILE* pf = fopen("Contact.txt", "w");
	if (pf == NULL)
	{
		perror("fopen");
		exit(-1);
	}
	//将数据写入文件中
	while (cur)
	{
		fprintf(pf,"%s %d %s %s %s\n",
			cur->data.name,
			cur->data.age,
			cur->data.sex,
			cur->data.phonenumber,
			cur->data.address);
		cur = cur->next;
	}
	//关闭文件
	fclose(pf);
	pf = NULL;
	printf("保存成功！");
}

//加载通讯录
void InitPeople()
{
	//打开文件
	FILE* pf = fopen("Contact.txt", "r");
	if (pf == NULL)
	{
		perror("fopen");
		exit(-1);
	}
	char arr[100] = { 0 };
	//读取文件信息
	while (fgets(arr, 100, pf))//遇到文件末尾，fgets会返回NULL
	{
		SingleListNode* cur = (SingleListNode*)malloc(sizeof(SingleListNode));
		if (cur == NULL)
		{
			perror("malloc");
			exit(-1);
		}
		sscanf(arr, "%s %d %s %s %s\n",
			cur->data.name,
			&cur->data.age,
			cur->data.sex,
			cur->data.phonenumber,
			cur->data.address);
		cur->next = NULL;

		//尾插法
		//判断链表是否为空
		if (head == NULL)
		{
			//空链表
			head = cur;
		}
		else
		{
			//非空链表，将学生信息尾插到链表中
			//找到尾节点
			SingleListNode* tail = head;
			while (tail->next)
			{
				tail = tail->next;
			}
			tail->next = cur;
		}
	}
	printf("加载成功！\n");
}